package com.base.taskreports.common.utlis;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;




public class TableNameUtil {
    
    /**
     * 积分-送分表tbl_cqpnt_point_in_flow01
     */
    public static final String JFSF = "tbl_cqpnt_point_in_flow";
    
    /**
     * 积分-用分tbl_cqpnt_point_out_flow01
     */
    public static final String JFYF = "tbl_cqpnt_point_out_flow";
    
    /**
     * 票券-领取tbl_coupon_opera_flow_01
     */
    public static final String PQLQ = "tbl_coupon_opera_flow_";
    
    /**
     * 票券-承兑tbl_discount_trans_01
     */
    public static final String PQCD = "tbl_discount_trans_";
    
    /**
     * 日期格式 年 月 日 如2009-02-26
     */
    public static final String DDAY = "dd";
    
    /**
     * 日期格式 年 月 日 如2009-02-26
     */
    public static final String DATEFORMATDAY = "yyyy-MM-dd";
    
    /**
     * 日期格式 年 月 日 如20090226
     */
    public static final String DATEFyyyymmddDAY = "yyyyMMdd";
    /**
    
    /**
     * 按指定的格式，把string转换成Date 如string为空或null，返回null
     * 
     * @param string
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parase(String string, String format) throws ParseException {
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        return new SimpleDateFormat(format).parse(string);
    }
    
    
    
    /**
     * 获取指定日期 前后的日期 --DD
     * 
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getAssignDay(Date someDate,int day) throws ParseException {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, day);
        return format(ca.getTime(), DDAY);
       // return parase(str, DATEFORMATDAY);
    }
    
    
    /**
     * 获取指定日期 前后的日期 -- DATEFyyyymmddDAY
     * 
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getAssignyyyymmddDay(Date someDate,int day) throws ParseException {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, day);
        return format(ca.getTime(), DATEFyyyymmddDAY);
       // return parase(str, DATEFORMATDAY);
    }
    
    
    /**
     * //获取指定日期的前后天数 +拼接表名
     * @throws ParseException 
     *
     */
    public static String getAssignTabName(String table,Date someDate,int day) throws ParseException {
        //获取指定日期的前后天数
    	String assignDay = getAssignDay(someDate,day);
    	String dayFirstSecond = table + assignDay;
		return dayFirstSecond;
    }
    
    /**
     * 获取昨天日期
     *
     */
    public static String getDayTabName(String table) {
        //获取日期
    	String dayFirstSecond = table + getYestodayHHMM();
		return dayFirstSecond;
    }
    
    
    /**
     * 获取前天日期
     *
     */
    public static String getAssignDayTabName(String table) {
        //获取日期
    	String dayFirstSecond = table + getQiandayHHMM();
		return dayFirstSecond;
    }
    
    

    /**
     * JFSF 表名+获取当前日期
     *
     */
    public static String getJFSFDayTabName() {
        //获取日期
    	String dayFirstSecond = JFSF + getDayFirstSecond(new Date());
		return dayFirstSecond;
    }
    
    /**
     *  JFYF 表名+获取当前日期
     *
     */
    public static String getJFYFDayTabName() {
        //获取日期
    	String dayFirstSecond = JFYF + getDayFirstSecond(new Date());
		return dayFirstSecond;
    }
    /**
     * PQLQ 表名+获取当前日期
     *
     */
    public static String getPQLQDayTabName() {
        //获取日期
    	String dayFirstSecond = PQLQ + getDayFirstSecond(new Date());
		return dayFirstSecond;
    }
    
    /**
     * PQCD 表名+获取当前日期
     *
     */
    public static String getPQCDDayTabName() {
        //获取日期
    	String dayFirstSecond = PQCD + getDayFirstSecond(new Date());
		return dayFirstSecond;
    }
    
    
    /**
     * JFSF 表名+获取昨天日期
     *
     */
    public static String getJFSFZDayTabName() {
        //获取日期
    	String dayFirstSecond = JFSF + getYestodayHHMM();
		return dayFirstSecond;
    }
    
    /**
     *  JFYF 表名+获取昨天日期
     *
     */
    public static String getJFYFZDayTabName() {
        //获取日期
    	String dayFirstSecond = JFYF + getYestodayHHMM();
		return dayFirstSecond;
    }
    /**
     * PQLQ 表名+获取昨天日期
     *
     */
    public static String getPQLQZDayTabName() {
        //获取日期
    	String dayFirstSecond = PQLQ + getYestodayHHMM();
		return dayFirstSecond;
    }
    
    /**
     * PQCD 表名+获取昨天日期
     *
     */
    public static String getPQCDZDayTabName() {
        //获取日期
    	String dayFirstSecond = PQCD + getYestodayHHMM();
		return dayFirstSecond;
    }
    
    
    /**
     * JFSF 表名+获取前天日期
     *
     */
    public static String getJFSFQDayTabName() {
        //获取日期
    	String dayFirstSecond = JFSF + getQiandayHHMM();
		return dayFirstSecond;
    }
    
    /**
     *  JFYF 表名+获取前天日期
     *
     */
    public static String getJFYFQDayTabName() {
        //获取日期
    	String dayFirstSecond = JFYF + getQiandayHHMM();
		return dayFirstSecond;
    }
    /**
     * PQLQ 表名+获取前天日期
     *
     */
    public static String getPQLQQDayTabName() {
        //获取日期
    	String dayFirstSecond = PQLQ + getQiandayHHMM();
		return dayFirstSecond;
    }
    
    /**
     * PQCD 表名+获取前天日期
     *
     */
    public static String getPQCDQDayTabName() {
        //获取日期
    	String dayFirstSecond = PQCD + getQiandayHHMM();
		return dayFirstSecond;
    }
    
    
    
    /**
     * JFSF 表名+获取指定日期
     *
     */
    public static String getJFSFAssignDayTabName(int day) {
        //获取日期
    	String dayFirstSecond = JFSF + getZDdayHHMM(day);
		return dayFirstSecond;
    }
    
    /**
     *  JFYF 表名+获取指定日期
     *
     */
    public static String getJFYFAssignDayTabName(int day) {
        //获取日期
    	String dayFirstSecond = JFYF + getZDdayHHMM(day);
		return dayFirstSecond;
    }
    /**
     * PQLQ 表名+获取指定日期
     *
     */
    public static String getPQLQAssignDayTabName(int day) {
        //获取日期
    	String dayFirstSecond = PQLQ + getZDdayHHMM(day);
		return dayFirstSecond;
    }
    
    /**
     * PQCD 表名+获取指定日期
     *
     */
    public static String getPQCDAssignDayTabName(int day) {
        //获取日期
    	String dayFirstSecond = PQCD + getZDdayHHMM(day);
		return dayFirstSecond;
    }
    
    
    /**
     * 按指定的格式，把Date转换成String 如date为null,返回null
     * 
     * @param date
     *            Date参数
     * @param format
     *            日期格式
     * @return String
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(format).format(date);
    }
    
    /**
     * 取一天的开始时间 精确到秒 如果date为null，返回null
     * 
     * @param date
     * @return
     * @throws Exception
     */
    public static String getDayFirstSecond(Date date) {
        if (date == null) {
            return null;
        }
        String str = format(date, DDAY);
        return str;
    }
    
    
    /**
     * 获取昨天的日期 dd
     * 
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getYestodayHHMM(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, -1);
        return format(calendar.getTime(), DDAY);
    }
    
    
    /**
     * 获取前天的日期 dd
     * 
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getQiandayHHMM()  {
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, -2);
        return format(calendar.getTime(), DDAY);
    }
    
    /**
     * 获取指定的日期 dd  -1 -2  -2
     * 
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getZDdayHHMM(int day)  {
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, -day);
        return format(calendar.getTime(), DDAY);
    }
    

    public static void main(String[] args) throws ParseException {
    	TableNameUtil tableNameUtil2 = new TableNameUtil();
    	String DateTime ="2021-11-12";
    	String table ="abbb";
    	Date string2Date = DateUtil.string2Date(DateTime);
    	String jfsfzDayTabName = tableNameUtil2.getAssignTabName(table,string2Date,-1);
    	String assignjfsfqDayTabName = tableNameUtil2.getAssignTabName(table,string2Date,-2);
    	String yestoyyyymmddday = tableNameUtil2.getAssignyyyymmddDay(string2Date,-1);
    	String assignYestoyyyymmddday = tableNameUtil2.getAssignyyyymmddDay(string2Date,-2);
		
		
		String format = new SimpleDateFormat("dd").format(new Date());
		
		TableNameUtil tableNameUtil = new TableNameUtil();
		String getYestodayHHMM = TableNameUtil.getYestodayHHMM();
		System.out.println(getYestodayHHMM);
		String qiandayHHMM = TableNameUtil.getQiandayHHMM();
		System.out.println(qiandayHHMM);
		
}
  //工具类
  	public static Map<String,String> dayUtlis(String DateTime,String table){
  		HashMap<String,String> hashMap = new HashMap<String, String>();
  		
     	// 动态表名  昨天
  	    String jfsfzDayTabName = null;
  	    // 动态表名  前天
  		String assignjfsfqDayTabName = null;
  	   // 昨天日期 yyyymmdd
		String yestoyyyymmddday = null;
		// 前天日期 yyyymmdd		
		String assignYestoyyyymmddday = null;	
  		if(!StringUtils.isEmpty(DateTime)) {
  			try {
				Date string2Date = DateUtil.string2Date(DateTime);
				jfsfzDayTabName = getAssignTabName(table,string2Date,-1);
				//assignjfsfqDayTabName = getAssignTabName(table,string2Date,-2);
				yestoyyyymmddday = getAssignyyyymmddDay(string2Date,-1);
				//assignYestoyyyymmddday = getAssignyyyymmddDay(string2Date,-2);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
  			
  		}else {
		    // 动态表名  昨天
		    jfsfzDayTabName = TableNameUtil.getDayTabName(table);
			// 动态表名  前天
		    //assignjfsfqDayTabName = TableNameUtil.getAssignDayTabName(table);
			try {
				 yestoyyyymmddday = DateUtil.getAssignYestoyyyymmddday(1);
				 //assignYestoyyyymmddday = DateUtil.getAssignYestoyyyymmddday(2);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
  		}
  		
  		
  		hashMap.put("jfsfzDayTabName", jfsfzDayTabName);
  		hashMap.put("assignjfsfqDayTabName", assignjfsfqDayTabName);
  	     //货币类型
  		String pointTp= "42";
  	     //货币类型
  		String pointTpOne= "41";
  		//货币状态
  		String floSt = "10000";
  		String bussTp = "4002";
  		hashMap.put("yestoyyyymmddday", yestoyyyymmddday);
  		hashMap.put("assignYestoyyyymmddday", assignYestoyyyymmddday);
  		hashMap.put("pointTp", pointTp);
  		hashMap.put("floSt", floSt);
  		hashMap.put("pointTpOne", pointTpOne);
  		hashMap.put("bussTp", bussTp);
  		return hashMap;
  		
  	}
}